/**
 * 时间格式取年月日
 * @param time
 * @returns {*}
 */
function getYearMonthDate(time) {
    // console.log(time);
    if(time!=null) {
        var YMD = time.split(" ")[0];  // 年月日
        var HMS = time.split(" ")[1];  // 时分秒
        return YMD;
    }
    return null;
}
layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging', 'form', 'upload'], function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer, //获取当前窗口的layer对象
        form = layui.form();
    var image = {};
    //首先渲染
    form.render();
    paging.init({
        openWait: true,
        url: '/recruitInfo/findByCondition2',
        elem: '#content', //内容容器
        params: { //发送到服务端的参数
            schoolid:magfacultyInfo.schoolid,
            isTemporary:1,
            pulisherid:''
        },
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        tempElem: '#tpl', //模块容器
        pageConfig: { //分页参数配置
            elem: 'paged', //分页容器
            pageSize: 10 //分页大小
        },
        success: function () { //渲染成功的回调
            //alert('渲染成功');
        },
        fail: function (msg) { //获取数据失败的回调
            //alert('获取数据失败')
        },
        complate: function () { //完成的回调
            //alert('处理完成');
            //绑定所有编辑按钮事件
            $('#content').children('tr').each(function () {
                //获取每行记录的id值
                var $that = $(this).children('td:last-child');
                var id = $that.children('a[data-opt=more]').data('id');
                //审核

                $that.children('a[data-opt=more]').on('click', function () {
                    axios.get('/recruitInfo/find/' + id)
                        .then(function (response) {
                            console.log(response.data.data);
                            vm.recruitinfo = response.data.data;
                            vm.positionType = positionTypes[vm.recruitinfo.positiontype];
                            console.log(vm.recruitinfo.job);
                            var divshow = $("#job");
                            divshow.text("");// 清空数据
                            divshow.html(vm.recruitinfo.job);
                            // console.log(positionTypes);
                            // console.log(vm.recruitinfo);
                            // form.render('select');
                            // $('select[name=type]').next().find('[lay-value="' + vm.recruitinfo.type + '"]').click();
                            // $('select[name=positiontype]').next().find('[lay-value="' + vm.recruitinfo.positiontype + '"]').click();
                            if (vm.recruitinfo.workingpic === 0) {
                                $('#showWorkingPic').attr('src', "");
                                vm.show = false;
                            }
                            else {
                                axios.get("/image/find/" + vm.recruitinfo.workingpic)
                                    .then(function (response) {
                                        $('#showWorkingPic').attr('src', response.data.data);
                                        vm.show = false;
                                    })
                                    .catch(function (response) {
                                        console.log(response);
                                    })
                            }

                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                });
            });

        }
    });
    var vm = new Vue({
        el: '#Vue',
        data: {
            show: true,
            recruitinfo: {
                publisherid: userId
            },
            positionType:""
        },

        methods: {
            /**
             * 不通过按钮
             */
            nopass: function () {
                vm.recruitinfo.state = 4;//未提交
                vm.saveOrUpdate();
            },
            /**
             * pass按钮
             */
           pass: function () {
                vm.recruitinfo.state = 3;//待审核
                vm.saveOrUpdate();
            },
            /**
             * 返回按钮
             */
            reload: function () {
                paging.get();
                vm.show = true;
                vm.clearData();
            },
            /**
             * 插入或更新数据库记录
             */
            saveOrUpdate: function () {
                var url = vm.recruitinfo.id == null ? "/recruitInfo/add" : "/recruitInfo/modify";
                console.log(vm.recruitinfo);
                axios.post(url, vm.recruitinfo)
                    .then(function (response) {
                        layer.msg(response.data.message, {time: 1500}, function () {
                            if (response.data.success) {
                                vm.reload();
                            }
                        });
                    })
                    .catch(function (response) {
                        console.log(response);
                        vm.clearData();
                    });
            },
            /**
             * 清除脏数据
             */
            clearData: function () {
                //重置数据并清除脏数据 默认为提交状态
                vm.recruitinfo = {state: 2, publisherid: userId};
                //清除照片src路径
                $('#showWorkingPic').attr('src', "");
                //重新渲染表单
                form.render();
            },
            myRecruit:function () {
                console.log("myRecruit");
                paging.get({schoolid:'',publisherid:userId,isTemporary:-1});
            },
            allRecruit:function () {
                console.log("allRecruit");
                paging.get({schoolid:magfacultyInfo.schoolid,isTemporary:1,publisherid:''});
            }
        },

    });

    //监听岗位类别
    form.on('select(positionType)', function (data) {
        vm.recruitinfo.positiontype = data.value;
    });
    //监听招聘类型
    // form.on('select(type)', function (data) {
    //     vm.recruitinfo.type = data.value;
    // });
    layui.upload({
        url: '/upload/workingpic',
        success: function (response) {
            image.path = response.data;
            image.description = "工作环境";
            image.isencrypted = 2;//需要加密
            axios.post("/image/add", image)
                .then(function (response) {
                    console.log(response.data);
                    vm.recruitinfo.workingpic = response.data.data;
                    axios.get("/image/find/" + response.data.data)
                        .then(function (response) {
                            $('#showWorkingPic').attr('src', response.data.data);
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                })
                .catch(function (response) {
                    console.log(response);
                })

        },
        before: function () {
            console.log('工作环境上传中');
        }
    });

});
